package au.edu.swin.swinvehicle.managers;

/**
 * VehicleJSFManagedBean
 * @version 1.0
 * @author Viet Bui
 * @lastmodify 2009-05-11
 */
import au.edu.swin.swinvehicle.vehicle.VehicleDetailedTO;
import au.edu.swin.swinvehicle.vehicle.VehicleManagerException;
import au.edu.swin.swinvehicle.vehicle.VehicleManagerRemote;
import au.edu.swin.swinvehicle.vehiclelicencetype.VehicleLicenceTypeManagerException;
import au.edu.swin.swinvehicle.vehiclelicencetype.VehicleLicenceTypeManagerRemote;
import au.edu.swin.swinvehicle.vehiclelicencetype.VehicleLicenceTypeTO;
import au.edu.swin.swinvehicle.vehicletype.VehicleTypeManagerException;
import au.edu.swin.swinvehicle.vehicletype.VehicleTypeManagerRemote;
import au.edu.swin.swinvehicle.vehicletype.VehicleTypeTO;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

public class VehicleJSFManagedBean {

    @EJB
    private VehicleManagerRemote vehicleManagerBean;
    @EJB
    private VehicleTypeManagerRemote vehicleTypeManagerBean;
    @EJB
    private VehicleLicenceTypeManagerRemote vehicleLicenceTypeManagerBean;
    private VehicleDetailedTO vehicle;

    public VehicleJSFManagedBean() {
        this.vehicle = new VehicleDetailedTO();
    }

    public VehicleDetailedTO getVehicle() {
        return vehicle;
    }

    public void setVehicle(VehicleDetailedTO vehicle) {
        this.vehicle = vehicle;
    }

    public String addVehicle() {
        try {
            this.vehicle.setDeleted(Boolean.FALSE);
            this.vehicleManagerBean.addVehicle(this.vehicle);
            // TODO: Pass the success message back to view
            return "listVehicles";
        } catch (VehicleManagerException ex) {
            Logger.getLogger(VehicleJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            // TODO: Pass the error message back to view
            return "addVehicle";
        }
    }

    public List<VehicleDetailedTO> getVehicles() {
        List<VehicleDetailedTO> vehicles = new ArrayList<VehicleDetailedTO>();
        try {
            vehicles = this.vehicleManagerBean.getAllDetailedVehicles();
        } catch (VehicleManagerException ex) {
            Logger.getLogger(VehicleJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return vehicles;
    }

    public List<SelectItem> getVehicleTypes() {
        List<SelectItem> selectItems = new ArrayList<SelectItem>();
        selectItems.add(new SelectItem(null, "--- Select vehicle type ---"));
        try {
            List<VehicleTypeTO> vehicleTypes = this.vehicleTypeManagerBean.getAllVehicleTypes();
            for (VehicleTypeTO vehicleType : vehicleTypes) {
                selectItems.add(new SelectItem(vehicleType.getId(), vehicleType.getCode()));
            }
        } catch (VehicleTypeManagerException ex) {
            Logger.getLogger(VehicleJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return selectItems;
    }

    public List<SelectItem> getVehicleLicenceTypes() {
        List<SelectItem> selectItems = new ArrayList<SelectItem>();
        selectItems.add(new SelectItem(null, "--- Select vehicle licence type ---"));
        try {
            List<VehicleLicenceTypeTO> vehicleLicenceTypes = this.vehicleLicenceTypeManagerBean.getAllVehicleLicenceTypes();
            for (VehicleLicenceTypeTO vehicleLicenceType : vehicleLicenceTypes) {
                selectItems.add(new SelectItem(vehicleLicenceType.getId(), vehicleLicenceType.getCode()));
            }
        } catch (VehicleLicenceTypeManagerException ex) {
            Logger.getLogger(VehicleJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return selectItems;
    }

    public String editVehicle() {
        String vehicleIdString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("vehicleId");
        try {
            Long vehicleId = Long.parseLong(vehicleIdString);
            this.vehicle = this.vehicleManagerBean.getVehicle(vehicleId);
            if (this.vehicle == null) {
                // TODO: Show error message
                return "listVehicles";
            } else {
                return "editVehicle";
            }
        } catch (VehicleManagerException ex) {
            Logger.getLogger(VehicleJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            return "listVehicles";
        }
    }

    public String saveVehicle() {
        try {
            this.vehicleManagerBean.editVehicle(this.vehicle);
            // TODO: Pass the success message back to view
            return "listVehicles";
        } catch (VehicleManagerException ex) {
            Logger.getLogger(VehicleJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            // TODO: Pass the error message back to view
            return "editVehicle";
        }
    }

    public String deleteVehicle() {
        String vehicleIdString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("vehicleId");
        try {
            Long vehicleId = Long.parseLong(vehicleIdString);
            this.vehicleManagerBean.deleteVehicle(vehicleId);
            // TODO: Display the success message
            return "listVehicles";
        } catch (VehicleManagerException ex) {
            Logger.getLogger(VehicleJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            // TODO: Pass the error message back to view
            return "listVehicles";
        }
    }
}